﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:SysData
    /// </summary>
    public partial class SysData
    {
        public SysData()
        {
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.SysData model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into SysData(");
            strSql.Append("SubName,SubValue,SubComment)");
            strSql.Append(" values (");
            strSql.Append("@SubName,@SubValue,@SubComment)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@SubName", SqlDbType.VarChar, 30),
                    new SqlParameter("@SubValue", SqlDbType.VarChar, 100),
                    new SqlParameter("@SubComment", SqlDbType.VarChar, 100) };
            parameters[0].Value = model.SubName;
            parameters[1].Value = model.SubValue;
            parameters[2].Value = model.SubComment;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.SysData model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update SysData set ");
            strSql.Append("SubName=@SubName,");
            strSql.Append("SubValue=@SubValue,");
            strSql.Append("SubComment=@SubComment");
            strSql.Append(" where SysNo=@SysNo");
            var parameters = new SqlParameter[] { new SqlParameter("@SubName", SqlDbType.VarChar, 30),
                    new SqlParameter("@SubValue", SqlDbType.VarChar, 100),
                    new SqlParameter("@SubComment", SqlDbType.VarChar, 100),
                    new SqlParameter("@SysNo", SqlDbType.Int, 4) };
            parameters[0].Value = model.SubName;
            parameters[1].Value = model.SubValue;
            parameters[2].Value = model.SubComment;
            parameters[3].Value = model.SysNo;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int SysNo)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from SysData ");
            strSql.Append(" where SysNo=@SysNo");
            var parameters = new SqlParameter[] { new SqlParameter("@SysNo", SqlDbType.Int, 4)
            };
            parameters[0].Value = SysNo;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string SysNolist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from SysData ");
            strSql.Append(" where SysNo in (" + SysNolist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.SysData GetModel(int SysNo)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 SysNo,SubName,SubValue,SubComment from SysData ");
            strSql.Append(" where SysNo=@SysNo");
            var parameters = new SqlParameter[] { new SqlParameter("@SysNo", SqlDbType.Int, 4)
            };
            parameters[0].Value = SysNo;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.SysData DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.SysData();
            if (row != null)
            {
                if (row["SysNo"] != null && row["SysNo"].ToString() != string.Empty)
                {
                    model.SysNo = int.Parse(row["SysNo"].ToString());
                }
                if (row["SubName"] != null)
                {
                    model.SubName = row["SubName"].ToString();
                }
                if (row["SubValue"] != null)
                {
                    model.SubValue = row["SubValue"].ToString();
                }
                if (row["SubComment"] != null)
                {
                    model.SubComment = row["SubComment"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select SysNo,SubName,SubValue,SubComment ");
            strSql.Append(" FROM SysData ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" SysNo,SubName,SubValue,SubComment ");
            strSql.Append(" FROM SysData ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SysData ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.SysNo desc");
            }
            strSql.Append(")AS Row, T.*  from SysData T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

